package user;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Killian
 */
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
 
 
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
 
import util.HibernateUtil;
 
public class UserDAO extends HibernateUtil {
    
    /**
     * Add or update an user
     * @param user
     * @return user
     */
    public User saveOrUpdateUser(User user) {
        System.out.println(user);
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        session.saveOrUpdate(user);
        session.getTransaction().commit();
        return user;
    }
    
    /**
     * Delete an user with the param id 
     * @param id_user
     * @return 
     */
    public User deleteUser(int id_user) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        User user = (User) session.load(User.class, id_user);
        if(null != user) {
            session.delete(user);
        }
        session.getTransaction().commit();
        return user;
    }
    /**
     * Get users db
     * @return listUser 
     */
    public List<User> listUsers() {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List<User> listUser = null;
        try {
             
            listUser = (List<User>)session.createQuery("from User").list();
             
        } catch (HibernateException e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        }
        session.getTransaction().commit();
        return listUser;
    }
    
    /**
     * Get user by identifiant
     * @param identifiant
     * @return User
     */ 
    User getUser(String identifiant) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Criteria criteria = session.createCriteria(User.class)
                .setFetchMode("Droit",FetchMode.JOIN);
        if (identifiant != null) {
            criteria.add(Restrictions.eq("identifiant", identifiant));
            User user = (User) criteria.uniqueResult();
            session.getTransaction().commit();
            return user;
        }
        session.getTransaction().commit();
        return null;
    }
    
    /**
     * Get User by id
     * @param userId
     * @return User
     */
    public User getUserById(int userId) {
        User user = null;
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        try {
            user = (User) session.get(User.class, userId);
            System.out.println(user);
        } catch (Exception e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        }
        session.getTransaction().commit();
        return user;
    }
    
    /**
     * check if the old passworld is correct when an user change her password
     * @param userId id of user connected
     * @param oldPassword
     * @return true if the password is correct
     * false else
     */
    boolean checkPassword(int userId, String oldPassword) {
        User user = getUserById(userId);
        return user.getPassword().equals(oldPassword);
    }
}